/**
 * @CopyRight weitaming.com
 * @author wuyahui
 * @E-mail wuyahui@ispied.com
 * @version 1.0.0
 * @Date  2014年5月6日 上午11:12:52
 */
package com.tstudy.laoshibang.manager;

import java.sql.SQLException;
import java.util.List;

import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.db.DBManagerImpl;
import com.tstudy.laoshibang.mode.User;
@SuppressWarnings("unchecked")
public class UserManager extends DBManagerImpl {
	private UserManager() {
	}

	public static UserManager instance = new UserManager(User.class);

	private UserManager(Class<User> clazz) {
		super(clazz);
	}

	public static UserManager getInstance() {
		return instance;
	}

	public void loginAction(User user){
		try {
			User dbUser = (User) dao.queryForId(user.getUserNo());
			if (dbUser != null) {
				user = dbUser;
			}
			user.setLogin(1);
			BaseApplication.mUserNo = user.userNo;
			BaseApplication.mCurrentUser = user;
			user.allDicStr();
			insertOrUpdate(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void logoutAction(String userNo){
		try {
			User user = (User) dao.queryForId(userNo);
			if (user != null) {
				user.setLogin(0);
				user.allDicStr();
				update(user);
			}
			BaseApplication.mCurrentUser = null;
			BaseApplication.mUserNo = null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public User getLoginUser(){
		User user = null;
		try {
			List<User> list = dao.queryForEq("isLogin", 1);
			if (list != null && list.size() > 0) {
				user = list.get(0);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (user != null) {
			user.parseAllDicMap();
		}
		return user;
	}

}
